$(function () {
    //加载弹出层
    layui.use(['form','element'],
        function() {
            layer = layui.layer;
            element = layui.element;
        });

    //触发事件
    var tab = {
        tabAdd: function(title,url,id){
            //新增一个Tab项
            element.tabAdd('xbs_tab', {
                title: title
                ,content: '<iframe tab-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="x-iframe"></iframe>'
                ,id: id
            })
        },
        tabAddiframe: function(title,url,id){
            //新增一个Tab项
            top.element.tabAdd('xbs_tab', {
                title: title
                ,content: '<iframe tab-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="x-iframe"></iframe>'
                ,id: id
            });
        }
        ,tabDelete: function(othis){
            //删除指定Tab项
            element.tabDelete('xbs_tab', '44'); //删除：“商品管理”
            othis.addClass('layui-btn-disabled');
        },
        tabChange: function(id){
            //切换到指定Tab项
            element.tabChange('xbs_tab', id); //切换到：用户管理
        },
        tabChangeiframe: function(id){
            //切换到指定Tab项
            top.element.tabChange('xbs_tab', id); //切换到：用户管理
        }
    };
    tableCheck = {
        init:function  () {
            $(".layui-form-checkbox").click(function(event) {
                if($(this).hasClass('layui-form-checked')){
                    $(this).removeClass('layui-form-checked');
                    if($(this).hasClass('header')){
                        $(".layui-form-checkbox").removeClass('layui-form-checked');
                    }
                }else{
                    $(this).addClass('layui-form-checked');
                    if($(this).hasClass('header')){
                        $(".layui-form-checkbox").addClass('layui-form-checked');
                    }
                }

            });
        },
        getData:function  () {
            var obj = $(".layui-form-checked").not('.header');
            var arr=[];
            obj.each(function(index, el) {
                arr.push(obj.eq(index).attr('data-id'));
            });
            return arr;
        }
    };

    //开启表格多选
    tableCheck.init();

    /*迷你菜单*/
    $('.left-nav .left_open i').click(function(event) {
        if($('.left-nav').hasClass('left-nav-min')){
            $('.left-nav').animate({left: '0p'}, 200).removeClass('left-nav-min');
            $('.page-content').animate({left: '190px'}, 200);
            if($(window).width()<768){
                $('.page-content-bg').show();
            }
            $('.left_open img').attr('src','/static/images/left_menu_close.png')
        }else{
            $('.left-nav').animate({left: '0'}, 100).addClass('left-nav-min');
            $('.page-content').animate({left: '56px'}, 100);
            $('.page-content-bg').hide();
            $('.sub-menu').hide();
            $('.left_open img').attr('src','/static/images/left_menu_open.png');

        }
    });


    $('.page-content-bg').click(function(event) {
        $('.left-nav').animate({left: '-190px'}, 100);
        $('.page-content').animate({left: '56px'}, 100);
        $(this).hide();
    });

    $('.layui-tab-close').click(function(event) {
        $('.layui-tab-title li').eq(0).find('i').remove();
    });

    $("tbody.x-cate tr[fid!='0']").hide();
    // 栏目多级显示效果
    $('.x-show').click(function () {
        if($(this).attr('status')=='true'){
            $(this).html('&#xe625;');
            $(this).attr('status','false');
            cateId = $(this).parents('tr').attr('cate-id');
            $("tbody tr[fid="+cateId+"]").show();
        }else{
            cateIds = [];
            $(this).html('&#xe623;');
            $(this).attr('status','true');
            cateId = $(this).parents('tr').attr('cate-id');
            getCateId(cateId);
            for (var i in cateIds) {
                $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
            }
        }
    });

    //左侧菜单效果
    $('.left-nav #nav li').click(function (event) {
        var hrefCont= $(this).children('a').attr('data-id');
        if($(this).children('.sub-menu').length){
            if($(this).hasClass('open')){
                $(this).removeClass('open');
                $(this).find('.nav_right').html('<img src="/static/images/leftNav-right.png"/>');
                $(this).children('.sub-menu').stop().slideUp();
                $(this).siblings().children('.sub-menu').slideUp();
            }else{
                $(this).addClass('open');
                $(this).children('a').addClass('selected').find('.nav_right').html('<img src="/static/images/leftNav-dow.png"/>');
                $(this).children('.sub-menu').stop().slideDown();
                $(this).siblings().children('.sub-menu').stop().slideUp();
                $(this).siblings().find('.nav_right').html('<img src="/static/images/leftNav-right.png"/>');
                $(this).siblings().removeClass('open');
                $('.left-nav').animate({left: '0p'}, 200).removeClass('left-nav-min');
                $('.page-content').animate({left: '190px'}, 200);
            }
            /*$(this).addClass('open');
            $(this).children('a').addClass('selected').find('.nav_right').html('<img src="dist/images/leftNav-dow.png"/>');
            $(this).children('.sub-menu').stop().slideDown();
            $(this).siblings().children('.sub-menu').stop().slideUp();
            $(this).siblings().find('.nav_right').html('<img src="dist/images/leftNav-right.png"/>');
            $(this).siblings().removeClass('open');
            $('.left-nav').animate({left: '0p'}, 200).removeClass('left-nav-min');
            $('.page-content').animate({left: '190px'}, 200);*/

        }else{
            if(hrefCont==1){
                var url = $(this).children('a').attr('_href');
                var title = $(this).find('span').html();
                var index  = $('.left-nav #nav li').index($(this));

                for (var i = 0; i <$('.x-iframe').length; i++) {
                    if($('.x-iframe').eq(i).attr('tab-id')==index+1){
                        tab.tabChange(index+1);
                        event.stopPropagation();
                        return;
                    }
                }
                tab.tabAdd(title,url,index+1);
                tab.tabChange(index+1);
            }

        }
        event.stopPropagation();
    });

    //内页打开Tab
    $(document).on('click','.TabIframe',function (event) {
        var url = $(this).attr('_href');
        var title = $(this).find('span').html();
        var indexTab  = $(this).attr('id');
        var index  = top.$('.left-nav #nav li').length;
        var clickTab=index+indexTab;

        for (var i = 0; i <top.$('.x-iframe').length; i++) {
            if(top.$('.x-iframe').eq(i).attr('tab-id')==clickTab){
                tab.tabChangeiframe(index+indexTab);
                event.stopPropagation();
                return;
            }
        }
        tab.tabAddiframe(title,url,clickTab);
        tab.tabChangeiframe(clickTab);
    });

    //头部搜索打开Tab
  /*  $(document).on('click','.top-search button',function (event) {
        var url = $(this).attr('_href');
        var title = $(this).find('span').html();
        var indexTab  = $(this).attr('id');
        for (var i = 0; i <top.$('.x-iframe').length; i++) {
            if(top.$('.x-iframe').eq(i).attr('tab-id')==indexTab){
                tab.tabChangeiframe(index+indexTab);
                event.stopPropagation();
                return;
            }
        }
        tab.tabAddiframe(title,url,indexTab);
        tab.tabChangeiframe(indexTab);
    });*/



    /*左侧二级菜单点击样式*/
    $('.left-nav a').click(function (event) {
        $('.left-nav a').removeClass('selected');
        $(this).addClass('selected');
    });
    //点击系统首页
    $('#HomeNav').click(function () {$('#HomeTab').click();$('#layui-tab-item').removeClass('layui-show');$('#HomeCont').addClass('layui-show')});

    /*div仿单选组*/
    $('.radioSel a').click(function () {$(this).addClass('current').siblings().removeClass('current');});
    $('input.zTreeSelect-text').attr('autocomplete', 'off');




    //打开筛选器
    $(document).on('click','.tableFilter a',function () {
        $(this).parent().toggleClass('open');
        $('.tableOperating').removeClass('open');
        $('.TableRow').removeClass('open');
        $('.OperatingRow').removeClass('open');
    });



    //检索器清除
    $(document).on('click','#FilterClearBtn',function () {$('#treesearch').val('');});
    //检索器取消
    $(document).on('click','#FilterCancelBtn',function () {$('.tableFilter').removeClass('open');$('#treesearch').val('');});


    //打开操作
     $(document).on('click','.tableOperating h3',function () {
         $(this).parent().toggleClass('open');
         $('.TableRow').removeClass('open');
         $('.tableFilter').removeClass('open');
         $('.OperatingRow').removeClass('open');
     });

    $(document).on('click','.tableOperating a',function () {
        $('.tableOperating').removeClass('open');
        $('.OperatingRow').removeClass('open');
    });

    //打开选择字段

    $(document).on('click','.TableRow a',function () {
        $(this).parent().toggleClass('open');
        $('.tableFilter').removeClass('open');
        $('.tableOperating').removeClass('open');
        $('.OperatingRow').removeClass('open');
    });
    $(".RowList").click(function(e) {e ? e.stopPropagation() : event.cancelBubble = true;});
    
    //打开表格内的操作
    $(document).on('click','.OperatingRow a.title',function () {
        $('.OperatingRow').removeClass('open');
        $(this).parent().addClass('open');
        $(this).parent().parent().css({overflow:'inherit'});
        $(this).parent().parent().parent().addClass('hoverTD');
    });
    $(document).on('mouseover','.hoverTD',function () {$('.layui-table-grid-down').css({display:'none'})});

    //点击空白处隐藏层
    $('body').on('click',function(){
        $('.OperatingRow,.selectCheckbox').removeClass('open');

    });

    //选择用户标签
    $(document).on('click','#select-checkbox-input',function () {
        $('.selectCheckbox').toggleClass('open');
    });






    $(document).bind('click', function(e) {
        var e = e || window.event; //浏览器兼容性
        var elem = e.target || e.srcElement;
        while (elem) { //循环判断至跟节点，防止点击的是div子元素
            //console.log(elem.id);
            if (elem.id && (elem.id == 'tableFilter'|| elem.id == 'select-checkbox-cont' || elem.id == 'FilterCont' || elem.id == 'tableOperating'|| elem.id == 'OperatingList'|| elem.id == 'TableRow'  || elem.id == 'RowList' || elem.id.indexOf('treeDemo')>-1)) {
                return;
            }
            elem = elem.parentNode;

        }
        $('.tableFilter,.tableOperating,.TableRow').removeClass('open'); //点击的不是div或其子元素
    });
    //搜索框焦点
    $(document).on('focus','.tableSearch input',function () {$(this).parent().css('border-color','#58b6ec')});
    $(document).on('blur','.tableSearch input',function () {$(this).parent().css('border-color','#dcddde')})

});

var cateIds = [];
function getCateId(cateId) {
    $("tbody tr[fid="+cateId+"]").each(function(index, el) {
        id = $(el).attr('cate-id');
        cateIds.push(id);
        getCateId(id);
    });
}


//关闭弹窗
function closeBtn(){layer.closeAll();}
//成功提示
/*function success(content){
    top.layer.msg("<img src='dist/images/success.png'/><span>"+content+"</span>",{
        skin:'layerMsg',
        time:2000,
        end:function () {
            top.$(".page-content .layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload(true);//刷新当前标签
            top.layer.closeAll();
        }
    });
}*/

/**
 * 成功提示
 * @param content    提示内容
 * @param isRefresh  是否刷新页面，默为不刷新
 * @param callback   回调函数处理
 * @return
 */
function success(content,isRefresh,callback){
    top.layer.closeAll();
    top.layer.msg("<img src='/static/images/success.png'/><span>"+content+"</span>",{
        skin:'layerMsg',
        time:2000,
        end:function () {
            if(callback){
                callback();
            }
            if(isRefresh){
                top.$(".page-content .layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload(true);//刷新当前标签
            }
        }
    });
}


//确认成功
function successBtn(title,content){
    top.layer.open({
        type:1,
        fix: false, //不固定
        maxmin: true,
        move:false,
        area:'450px',
        btnAlign: 'r',
        skin:'layerTips2',
        shade:0.01,
        title:false,
        closeBtn:0,
        content: "" +
        "<div class='layerOpenCont'>" +
        "<div class='Layer-txt'>" +
        "<div class='icon'><img src='/static/images/success.png'/></div>" +
        "<div class='cont'>" +
        "<h3>"+title+"</h3>" +
        "<p>"+content+"</p>" +
        "</div>" +
        "</div>" +
        "</div>",
        btn:'确定',
        yes:function () {
            top.layer.closeAll();//关闭弹窗
            top.$(".page-content .layui-tab-item.layui-show").find("iframe")[0].contentWindow.location.reload(true);//刷新当前标签
            return false
        }
    })
}

//异常提示
function warning(content){
    top.layer.msg("<img src='/static/images/warning.png'/><span>"+content+"</span>",{
        skin:'layerMsg',
        time:2000
    });
}

//操作失败提示
function failure(title,content){
    top.layer.open({
        type:1,
        fix: false, //不固定
        maxmin: true,
        move:false,
        area:'450px',
        btnAlign: 'r',
        skin:'layerTips2',
        shade:0.01,
        title:false,
        closeBtn:0,
        content: "" +
        "<div class='layerOpenCont'>" +
        "<div class='Layer-txt'>" +
        "<div class='icon'><img src='/static/images/failure.png'/></div>" +
        "<div class='cont'>" +
        "<h3>"+title+"</h3>" +
        "<p>"+content+"</p>" +
        "</div>" +
        "</div>" +
        "</div>",
        btn:'确定' 
        /*yes:function () {
            top.layer.closeAll();//关闭弹窗
            return false
        }*/
    })
}




